package com.example.VolunteerBank.controller;

import com.example.VolunteerBank.pojo.Category;
import com.example.VolunteerBank.pojo.Result;
import com.example.VolunteerBank.service.CategoryService;
import com.example.VolunteerBank.utils.ThreadLocalUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@Slf4j
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    //文章分类列表
    @GetMapping
    public Result<List<Category>> listByUserID(){
        log.info("文章分类列表");
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("id");
        List<Category> categories = categoryService.listByUserId(userId);
        return Result.success(categories);
    }

    //新增文章分类
    @PostMapping
    public Result add(@RequestBody @Validated(Category.Add.class) Category category){
        log.info("新增文章分类：{}", category);
        categoryService.add(category);
        return Result.success();
    }

    //更新文章分类
    @PutMapping
    public Result update(@RequestBody @Validated(Category.Update.class) Category category){
        log.info("更新文章分类：{}", category);
        categoryService.update(category);
        return Result.success();
    }

    //获取文章分类详情
    @GetMapping("/detail")
    public Result<Category> categoryDetail(Integer id){
        log.info("获取文章分类详情：{}", id);
        Category category = categoryService.findById(id);
        return Result.success(category);
    }

    //删除文章分类
    @DeleteMapping
    public Result detele(Integer id){
        log.info("删除文章分类：{}", id);
        categoryService.delete(id);
        return Result.success();
    }
}
